package com.frxuexi.park2212.emp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.GlideUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.EmpBeanItem
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityEmpAddBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/EmpAddActivity")
@AndroidEntryPoint
class EmpAddActivity : BaseActivity<ActivityEmpAddBinding,EmpViewModel>() {
    var did=0
    var dname=""
    override fun initData() {
        vm.getDeparts.observe(this){bean->
            val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, bean.map { it.name })
            binding.empaddDepart.adapter=arrayAdapter
            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            binding.empaddDepart.onItemSelectedListener=object :OnItemSelectedListener{
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    did=bean.get(position).id
                    dname=bean.get(position).name
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {
                }

            }
        }

        vm.addEmp.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun initView() {
        binding.headEmpadd.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("人员添加")
            headerAdd.setImageResource(R.drawable.ic_action_ok)
            headerAdd.setOnClickListener {
                val email = binding.empaddEmail.text.toString()
                val name = binding.empaddName.text.toString()
                val item = EmpBeanItem(
                    "",
                    did,
                    email,
                    null,
                    name,
                    SPUtils.getInstance().getInt(Const.UID),
                    dname
                )
                vm.addEmp(item)
            }
        }

        vm.getDeparts()

        binding.empaddIv.setOnClickListener {
            changeImg()
        }
        binding.empaddChange.setOnClickListener {
            changeImg()
        }
    }

    fun changeImg(){
        PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val pictureBean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            GlideUtils.loadCircleImg(pictureBean?.path!!,binding.empaddIv)
        }
    }

}